package org.jxc.web.action;

import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.ArriveOrder;
import org.jxc.domain.ArriveOrderProduct;
import org.jxc.domain.PurchaseOrder;
import org.jxc.service.ArriveOrderProductService;
import org.jxc.service.ArriveOrderService;
import org.jxc.service.PurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class ArriveOrderAction extends GenericAction<ArriveOrder> {

	private static final long serialVersionUID = 3780914758042156697L;

	@Autowired
	private ArriveOrderService arriveOrderService;
	@Autowired
	private ArriveOrderProductService arriveOrderProductService;
	@Autowired
	private PurchaseOrderService purchaseOrderService;

	private String purchaseOrderId;
	private String arriveOrderId;
	private ArriveOrder arriveOrder;
	private List<ArriveOrderProduct> aopList;
	private List<Integer> amountsList;
	private List<String> aopIdList;
	private Integer auditState;

	public String save() throws Exception {
		ArriveOrder ao = arriveOrderService
				.findArriveOrderByPurchaseOrderId(purchaseOrderId);
		if (ao == null) {
			arriveOrderId = arriveOrderService.save(purchaseOrderId);
		} else {
			arriveOrderId = ao.getId();
		}
		return SUCCESS;
	}

	public String toUpdate() throws Exception {
		arriveOrder = arriveOrderService.findById(arriveOrderId);
		aopList = arriveOrderProductService.findByArriveOrderId(arriveOrderId);
		return SUCCESS;
	}

	public String update() throws Exception {
		arriveOrderService.update(arriveOrder);
		if (aopIdList != null) {
			for (int i = 0; i < aopIdList.size(); i++) {
				ArriveOrderProduct temp = arriveOrderProductService
						.findById(aopIdList.get(i));
				temp.setInAmounts(amountsList.get(i));
				temp.setPrice(temp.getPrice());
				arriveOrderProductService.updateArriveOrderProduct(temp);
			}
		}
		return SUCCESS;
	}

	public String listPagination() throws Exception {
		page = arriveOrderService.listPagination(page);
		return SUCCESS;
	}

	public String toView() throws Exception {
		arriveOrder = arriveOrderService.findById(arriveOrderId);
		aopList = arriveOrderProductService.findByArriveOrderId(arriveOrderId);
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		for (String str : idList) {
			try {
				arriveOrderService.deleteArriveOrder(str);
			} catch (Exception e) {
				continue;
			}
		}
		return SUCCESS;
	}

	public String apply() throws Exception {
		arriveOrder = arriveOrderService.findById(arriveOrderId);
		arriveOrder.setAuditState(JXCString.APPLY);
		arriveOrderService.update(arriveOrder);
		return SUCCESS;
	}

	public String audit() throws Exception {
		arriveOrder = arriveOrderService.findById(arriveOrderId);
		arriveOrder.setAuditState(auditState);
		PurchaseOrder po = arriveOrder.getPurchaseOrder();
		if (auditState == 2) {
			po.setArriveState(JXCString.ARRIVED);
		} else if (auditState == 3) {
			po.setArriveState(JXCString.NOTARRIVED);
		}
		purchaseOrderService.updatePurchaseOrder(po);
		arriveOrderService.update(arriveOrder);
		return SUCCESS;
	}

	public String getPurchaseOrderId() {
		return purchaseOrderId;
	}

	public void setPurchaseOrderId(String purchaseOrderId) {
		this.purchaseOrderId = purchaseOrderId;
	}

	public String getArriveOrderId() {
		return arriveOrderId;
	}

	public void setArriveOrderId(String arriveOrderId) {
		this.arriveOrderId = arriveOrderId;
	}

	public ArriveOrder getArriveOrder() {
		return arriveOrder;
	}

	public void setArriveOrder(ArriveOrder arriveOrder) {
		this.arriveOrder = arriveOrder;
	}

	public List<ArriveOrderProduct> getAopList() {
		return aopList;
	}

	public List<Integer> getAmountsList() {
		return amountsList;
	}

	public void setAmountsList(List<Integer> amountsList) {
		this.amountsList = amountsList;
	}

	public List<String> getAopIdList() {
		return aopIdList;
	}

	public void setAopIdList(List<String> aopIdList) {
		this.aopIdList = aopIdList;
	}

	public Integer getAuditState() {
		return auditState;
	}

	public void setAuditState(Integer auditState) {
		this.auditState = auditState;
	}
}
